<template>
  <div>
    <el-card>
      <el-form ref="query" :model="query" label-width="80px" :inline="true">
        <el-form-item>
          <el-input
            v-model="query.keyword"
            placeholder="请输入内容"
            class="input-with-select"
          >
            <el-button slot="append" icon="el-icon-search" />
          </el-input>
        </el-form-item>
      </el-form>

      <el-table :data="dataList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#" />
        <el-table-column prop="nick_name" label="用户昵称" />
        <el-table-column prop="avatar_url" label="头像">
          <template slot-scope="scope">
            <div v-if="scope.row.avatar_url" class="video_img">
              <img :src="scope.row.avatar_url">
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="mobile" label="手机号码" />
        <el-table-column prop="create_time" label="创建时间" />
      </el-table>

      <el-pagination
        :current-page="query.page"
        :page-size="query.pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <el-dialog :visible.sync="formVisible" :title="formTitle">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="100px"
      >
        <el-form-item prop="title" label="专栏标题">
          <el-input
            v-model="formData.title"
            placeholder="请输入专栏标题"
            type="text"
          />
        </el-form-item>
        <el-form-item prop="cover_url" label="封面图">
          <ImageUpload
            ref="imageupload"
            :init-image-url="formData.cover_url"
            @choose-image="uploadImage"
          />
        </el-form-item>
        <el-form-item prop="desc" label="专栏介绍">
          <el-input
            v-model="formData.desc"
            placeholder="请输入专栏介绍"
            type="text"
          />
        </el-form-item>
        <el-form-item prop="category_id" label="所属科室">
          {{ formExtra.category_id }}
          <el-button @click="openCategory">选择</el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeForm">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog :visible.sync="categoryVisible" title="选择科室">
      <div>
        <el-tree
          :data="categoryTree"
          :props="defaultProps"
          node-key="id"
          default-expand-all
          :expand-on-click-node="false"
        >
          <span slot-scope="{ node, data }" class="custom-tree-node">
            <span>{{ node.label }}</span>
            <span>
              <el-button
                size="mini"
                @click="() => chooseCategory(data)"
              >选择</el-button>
            </span>
          </span>
        </el-tree>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ImageUpload from '@/components/ImageUpload/index'
import { getUserList } from '@/api/user/index'
import { getCategoryTree } from '@/api/category/index'
import { createTopic, updateTopic } from '@/api/topic/index'
export default {
  components: {
    ImageUpload
  },
  data() {
    return {
      id: 0,
      query: {
        page: 1,
        pageSize: 10
      },
      dataList: [],
      total: 0,

      formTitle: '新增专栏',
      formVisible: false,
      formData: {
        title: '',
        cover_url: '',
        desc: '',
        category_id: 0
      },
      formExtra: {
        category_id: ''
      },
      formRules: {
        title: {
          required: true,
          message: '请输入专栏标题',
          trigger: 'blur'
        },
        cover_url: {
          required: true,
          message: '上传专栏封面图',
          trigger: 'blur'
        },
        category_id: {
          min: 1,
          message: '请选择分类',
          trigger: 'blur',
          type: 'number'
        }
      },

      categoryTree: [],
      defaultProps: {
        children: 'sub',
        label: 'name'
      },
      categoryVisible: false
    }
  },
  created() {
    this.getDataList()
  },
  methods: {
    async getDataList() {
      getUserList(this.query).then(res => {
        console.log(res)
        this.dataList = res.data.list
        this.total = res.data.total
      })
    },
    handleCurrentChange(page) {
      this.query.page = page
      this.getDataList()
    },
    closeForm() {
      this.formVisible = false
    },
    add() {
      this.formVisible = true
    },
    async openCategory() {
      this.categoryVisible = true
      const res = await getCategoryTree()
      this.categoryTree = res.data
    },
    chooseCategory(data) {
      this.formData.category_id = data.id
      this.formExtra.category_id = data.name
      this.categoryVisible = false
    },
    uploadImage(data) {
      this.formData.cover_url = data.url
    },

    submit() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) {
          this.$message.error('请完善必填项')
        }
        let res = {}
        if (this.id !== 0) {
          res = await updateTopic(this.formData, this.id)
        } else {
          res = await createTopic(this.formData)
        }

        if (res.code === 1) {
          this.$message.success(res.msg)
          this.formVisible = false
          this.getDataList()
        } else {
          this.$message.error(res.msg)
        }
        console.log(res)
      })
    }

    // async delVideo(videoId) {
    //   this.$confirm('是否确认删除, 是否继续?', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   })
    //     .then(async () => {
    //       const { data: res } = await this.$http.get(
    //         '/admin/video/deleteVideo',
    //         {
    //           params: { videoId }
    //         }
    //       )
    //       if (res.code === 1) {
    //         this.getVideoList()
    //         this.$message.success(res.msg)
    //       } else {
    //         this.$message.error(res.msg)
    //       }
    //     })
    //     .catch(res => {
    //       this.$message({
    //         type: 'info',
    //         message: '已取消删除'
    //       })
    //     })
    // }
  }
}
</script>

<style scoped>
.video_img img {
  width: 50px;
}
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 10px;
  margin: 10px;
}
</style>
